package com.example.day2.busi.login;

import android.content.Intent;
import android.view.View;
import android.widget.Toast;

import androidx.lifecycle.Observer;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.day2.MainActivity;
import com.example.day2.R;

import com.example.day2.databinding.ActivityLoginBinding;
import com.example.day2.entity.LoginParam;
import com.example.day2.entity.LoginResult;
import com.example.day2.mvvm.base.BaseActivity;
import com.example.day2.mvvm.utils.Const;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

import java.util.ArrayList;
import java.util.List;

public class LoginActivity extends BaseActivity<ActivityLoginBinding,LoginViewModel> {
    @Override
    public void initView() {
        int fale = SPUtils.getInstance().getInt("fale", 0);
        if (fale == 0){
            binding.btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LoginParam loginParam = new LoginParam();
                    loginParam.setUsername(binding.et1.getText().toString());
                    loginParam.setPassword(binding.et2.getText().toString());

//                if (binding.et1.getText().toString().length()<6){
//                    ToastUtils.showLong("用户名不符");
//                }else if(!RegexUtils.isMobileSimple(binding.et1.getText().toString())){
//                    ToastUtils.showLong("请输入正确的手机号");
//                }else{
                    viewModel.LoginViewModel(loginParam);
//                }

                }
            });
        }else{
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
        }
        EMClient.getInstance().logout(true);
     viewModel.success.observe(this, new Observer<LoginResult>() {
         @Override
         public void onChanged(LoginResult loginResult) {
             if (loginResult.getCode() == 200){

                 //登录
                 EMClient.getInstance().login(loginResult.getData().getUsername(), loginResult.getData().getPassword(), new EMCallBack() {
                     // 登录成功回调
                     @Override
                     public void onSuccess() {
                         ToastUtils.showLong("环信登录成功");
                         SPUtils.getInstance().put("username",loginResult.getData().getUsername());
                         SPUtils.getInstance().put(Const.TOKEN_KEY,loginResult.getData().getToken());
                         startActivity(new Intent(LoginActivity.this, MainActivity.class));
                         SPUtils.getInstance().put("nickname",loginResult.getData().getNickname());
                         SPUtils.getInstance().put("address",loginResult.getData().getAddress());
                         SPUtils.getInstance().put("birth", loginResult.getData().getBirth());
                         SPUtils.getInstance().put("icon",loginResult.getData().getIcon());
                         SPUtils.getInstance().put("sex",  loginResult.getData().getSex());
                         SPUtils.getInstance().put("fale",1);
                     }

                     // 登录失败回调，包含错误信息
                     @Override
                     public void onError(final int code, final String error) {
                        ToastUtils.showLong("环信登录失败");
                     }

                     @Override
                     public void onProgress(int i, String s) {

                     }

                 });


             }
         }
     });
     viewModel.fail.observe(this, new Observer<String>() {
         @Override
         public void onChanged(String s) {
             Toast.makeText(LoginActivity.this, "登录失败"+s, Toast.LENGTH_SHORT).show();
         }
     });





    }

    @Override
    public void initData() {

    }



    @Override
    public int bindLayoutId() {
        return R.layout.activity_login;
    }
    @Override
    public Class<LoginViewModel> getVIewModel() {
        return LoginViewModel.class;
    }
}
